<?php
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class CatesController extends AdminBaseController{
    public function index(){
        $title = I('get.title/s','');
        $page = I('get.page/d',1);
        $col = M('columns');
        $pageCount = 3 ;
        $whereSql = empty($title) ? '' : 'columns like "%'.$title.'%" ';
        $count      = $col->where($whereSql)->count();
        $countNum = ceil($count / $pageCount);
        $frRow = empty($page - 1) ? 0 : (($page - 1) * $pageCount);

        $list = $col->where($whereSql)->order('id desc')->limit($frRow,$pageCount)->select();
        $this->assign('list',$list);
        $this->assign('title',$title);
        $this->assign('countNum',$countNum);
        $this->display();
    }
    public function add(){
        $this->display();
    }
    public  function edit(){
        if(IS_GET) {
            $id = I("get.id/d", 0);
            if(empty($id)){
                $this -> redirect('Admin/columns/index');
            }
            $columnsInfo = M('Columns') -> field('id,pid,columns,status')->where(['id' => $id])->find();
            $colList = M('Columns')->field('id,columns')->where(['status = 1'])->select();
            $this->assign('list',$colList);
            $this->assign('columnsInfo', $columnsInfo);
            $this->display();
        }
    }
    public function save(){
        if(IS_AJAX || IS_POST){
            $id = I('post.id/d',0);
            if(!$id) {
                $columns = I('post.columns/s','');
                if(empty($columns)){
                    $this->ajaxReturn(['status' => 0, 'msg' => '栏目不能为空！']);
                }
                $status = I('post.status/d',1);
                $column = M('columns');
                $columnsInfo = $column-> where(['columns' => $columns]) -> find();
                if(!empty($columnsInfo)){
                    $this->ajaxReturn(['status' => 0, 'msg' => '栏目已存在！']);
                }
                $data['columns'] = $columns;
                $data['status'] = $status;
                $isADD = $column -> data($data) -> save();
                if(!empty($isADD)){
                    $this->ajaxReturn(['status' => 1, 'msg' => '添加成功！']);
                }
                $this->ajaxReturn(['status' => 0, 'msg' => '添加失败！']);
            }else{
                $columns = I('post.columns/s', '');
                if (!$columns) {
                    $this->ajaxReturn(['status' => 0, 'msg' => '栏目不能为空！']);
                }
                $col = M('columns');
                $colData = $col->where(['id' => $id])->find();
                if(empty($colData)){
                    $this->ajaxReturn(['status' => 0 , 'msg' => '栏目不存在！']);
                }
                $mid = $col->where(['id' => $id])->setField(['columns' => $columns]);
                if($mid === false){
                    $this->ajaxReturn(['status' => 0 , 'msg' => '更新失败！']);
                }
                $this->ajaxReturn(['status' => 1 , 'msg' => '更新成功！']);
            }
        }
    }
    public function columnsDel(){
        if(IS_POST){
            $id = I('post.id/d');
            $admin = M('columns');
            if(($id != 1) && !empty($id)){
                $map['id'] = $id;
                $admin -> where($map) -> delete();
                $this->ajaxReturn([ 'title' => '删除成功！','msg' => '您已经永久删除了这条信息。','status' => 'success']);
            }else{
                $this->ajaxReturn(['title' => '删除失败！','msg' => '非法操作！','status' => 'error']);
            }
        }else{
            $this->ajaxReturn(['title' => '删除失败！','msg' => '非法操作！','status' => 'error']);
        }
    }
    public function columnsChange(){
        if(IS_POST){
            $id = I('post.id/d');
            $admin = M('columns');
            if(($id != 1) && !empty($id)){
                $map['id'] = $id;
                $data = $admin -> field('status') -> where($map) -> find();
                if(!empty($data)){
                    if($data['status'] == 1 ){
                        $change['status'] = 0;
                        $admin -> where($map) -> save($change);
                        $this->ajaxReturn(['msg' => '禁用成功！','status' => 1,'state' => 0]);
                    }else{
                        $change['status'] = 1;
                        $admin -> where($map) -> save($change);
                        $this->ajaxReturn(['msg' => '启用成功！','status' => 1,'state' => 1]);
                    }
                }else{
                    $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
                }
            }else{
                $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
            }
        }else{
            $this->ajaxReturn(['msg' =>'非法操作','status' => 0]);
        }
    }
}